// store.js
import {applyMiddleware, compose, createStore} from 'redux';
import createSagaMiddleware from 'redux-saga';
import rootReducer, {history} from './reducers';
import rootSaga from './sagas';
import {routerMiddleware} from "connected-react-router";

// 创建 Redux Saga 中间件
const sagaMiddleware = createSagaMiddleware();
// 使用 composeWithDevTools 合并中间件和 Redux DevTools 插件
// @ts-ignore
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const configureStore = () => {
    const store = createStore(
        rootReducer,
        composeEnhancers(
            applyMiddleware(
                sagaMiddleware,
                routerMiddleware(history)
            )
        )
    );
    sagaMiddleware.run(rootSaga);

    return store;
}

export default configureStore;
